//
//  StateMachine.m
//  LearnObjectiveC
//
//  Created by client on 2017/9/18.
//  Copyright © 2017年 吴可高. All rights reserved.
//

#import "StateMachine.h"
#import "YYStateMachine.h"
@implementation StateMachine

static NSString * const kStateTypeInit = @"SSStateTypeInit";
static NSString * const kStateTypeCheckPayParameters = @"SSStateTypeCheckPayParameters";
static NSString * const kStateTypeCheckDeviceId = @"SSStateTypeCheckDeviceId";

//Event
static NSString *const kSEventTypeInit = @"SSEventTypeInit";
static NSString *const kSEventTypeCheckDeviceId = @"SSEventTypeCheckDeviceId";


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initializeStateMachine];
    }
    return self;
}

State_Machine_Macro(^(YYStateMachine *sm){
    
    sm.initialState = [StateMachine getStateTypeString:SSStateTypeInit];
    
    [sm addState:[StateMachine getStateTypeString:SSStateTypeInit]];
    [sm addState:[StateMachine getStateTypeString:SSStateTypeCheckPayParameters]];
    [sm addState:[StateMachine getStateTypeString:SSStateTypeCheckDeviceId]];

    [sm when:[StateMachine getEventTypeString:SSEventTypeInit] transitionForm:[StateMachine getStateTypeString:SSStateTypeInit] to:[StateMachine getStateTypeString:SSStateTypeCheckPayParameters]];
    
    [sm when:[StateMachine getEventTypeString:SSEventTypeCheckDeviceId] transitionForm:[StateMachine getStateTypeString:SSStateTypeCheckPayParameters] to:[StateMachine getStateTypeString:SSStateTypeCheckDeviceId]];
});

+ (NSString*)getStateTypeString:(SStateType)stateType;
{
    NSDictionary * config = @{@(SSStateTypeInit):kStateTypeInit,
                              @(SSStateTypeCheckPayParameters):kStateTypeCheckPayParameters,
                              @(SSStateTypeCheckDeviceId):kStateTypeCheckDeviceId};
    NSNumber * key = [NSNumber numberWithInteger:stateType];
    return config[key];
}

+ (SStateType)getStateTypeEnum:(NSString *)stateType;
{
    NSDictionary * config = @{kStateTypeInit:@(SSStateTypeInit),
                              kStateTypeCheckPayParameters:@(SSStateTypeCheckPayParameters),
                              kStateTypeCheckDeviceId:@(SSStateTypeCheckDeviceId)};
    return [config[stateType]integerValue];
}

+ (NSString *)getEventTypeString:(SEventType)eventType;
{
    NSDictionary * config = @{@(SSEventTypeInit):kSEventTypeInit,
                              @(SSEventTypeCheckDeviceId):kSEventTypeCheckDeviceId};
    NSNumber * key = [NSNumber numberWithInteger:eventType];
    return config[key];
}

- (BOOL)performEventWithEventType:(SEventType)eventType;
{
    BOOL result =NO;
    switch (eventType) {
        case SSEventTypeInit:
            result=[self SSEventTypeInit];
            break;
        case SSEventTypeCheckDeviceId:
            result=[self SSEventTypeCheckDeviceId];
            break;
        default:
            break;
    }
    return result;
}

@end
